package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookShelfMapper;
import com.kgc.novel.domain.entity.BookShelf;
import com.kgc.novel.domain.entity.BookTag;
import com.kgc.novel.domain.param.BookShelfParam;
import com.kgc.novel.domain.vo.BookManageVO;
import com.kgc.novel.domain.vo.BookShelfVO;
import com.kgc.novel.service.user.BookShelfService;
import com.kgc.novel.util.CutOut;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookShelfServiceImpl extends ServiceImpl<BookShelfMapper, BookShelf> implements BookShelfService {
    @Resource
    private BookShelfMapper bookShelfMapper;

    @Override
    public Res selectShelfAll(BookShelfParam bookShelfParam) {
        List<BookShelfVO> allShelf = bookShelfMapper.getAllShelf(bookShelfParam);
        Integer countShelf = bookShelfMapper.getCountShelf(bookShelfParam.getBookName(), bookShelfParam.getAuthorName(), bookShelfParam.getNickName(), bookShelfParam.getManageEnd());
        return Res.returnData(allShelf, (long) countShelf);
    }

    @Override
    public Res getSingleShelf(String id) {
        BookShelf bookShelf = bookShelfMapper.selectById(id);
        if (bookShelf != null) {
            return Res.returnData("200", bookShelf);
        }
        return Res.returnMsg("暂无该作者的信息！");
    }

    @Override
    public Res addShelf(BookShelf bookShelf) {
        LambdaQueryWrapper<BookShelf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookShelf::getBookId, bookShelf.getBookId());

        BookShelf bookShelf1 = bookShelfMapper.selectOne(lambdaQueryWrapper);
        if (bookShelf1 != null) {
            return Res.returnMsg("书本已经存在，请重新添加！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookShelf.setId(String.valueOf(id));
        bookShelfMapper.insert(bookShelf);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateShelf(BookShelf bookShelf) {
        LambdaQueryWrapper<BookShelf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookShelf::getBookId, bookShelf.getBookId());

        BookShelf bookShelf1 = bookShelfMapper.selectOne(lambdaQueryWrapper);
        if (bookShelf1 == null) {
            bookShelfMapper.updateById(bookShelf);
            return Res.returnMsg("修改成功！");
        }
        BookShelf bookShelf2 = bookShelfMapper.selectById(bookShelf.getId());
        if (bookShelf2.getId().equals(bookShelf.getId())) {
            bookShelfMapper.updateById(bookShelf);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnMsg("书本已经存在，请重新修改！");
    }

    @Override
    public Res deleteShelf(String id) {
        int i = bookShelfMapper.deleteById(id);
        if (i != -1) {
            return Res.returnMsg("删除成功！");
        } else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllShelf(List<String> ids) {
        int i = bookShelfMapper.deleteBatchIds(ids);
        if (i != -1) {
            return Res.returnMsg("批量删除成功");
        } else {
            return Res.returnMsg("批量删除失败");
        }
    }
}
